package com.chinapost.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.beans.factory.annotation.Autowired;

import com.chinapost.excelUtil.ExcelUtils;
import com.chinapost.excelUtil.JsGridReportBase;
import com.chinapost.excelUtil.TableData;
import com.chinapost.service.DeptServiceI;
import com.chinapost.service.UserServiceI;
import com.chinapost.service.YcsjServiceI;
import com.chinapost.vo.SessionInfo;
import com.chinapost.vo.Ycsj;
import com.opensymphony.xwork2.ModelDriven;

@Action(value = "ycsjAction", results = { 
		@Result(name = "ycsjManager", location = "/jsp/ycsj/ycsjManager.jsp"),
		@Result(name = "manager", location = "/jsp/ycsj/manager.jsp")
		})
public class YcsjAction extends BaseAction implements ModelDriven<Ycsj>, ServletRequestAware,ServletResponseAware {

	private Ycsj ycsj = new Ycsj();
	private HttpServletRequest request;
	private HttpServletResponse response;
	@Autowired
	private YcsjServiceI ycsjService;
	@Autowired
	private DeptServiceI deptService;
	@Autowired
	private UserServiceI userService;

	@Override
	public Ycsj getModel() {
		return ycsj;
	}

	public String ycsjManager() {
		SessionInfo si=(SessionInfo)ServletActionContext.getRequest().getSession().getAttribute("sessionInfo");
		String roleIds = si.getRoleIds();
		if (roleIds.contains("9999")){//普通用户的角色ID是9999
			return "manager";//普通用户返回manager.jsp页面
		}else{
			return "ycsjManager";
		}
	}

	
	public void datagrid() {
		if (!StringUtils.isEmpty(ycsj.getDeptid())) {
			ycsj.setDeptIds(deptService.getChildIds(ycsj.getDeptid()));
		} else {
			SessionInfo s = (SessionInfo) request.getSession().getAttribute("sessionInfo");
			ycsj.setDeptIds(deptService.getChildIds(s.getDeptid()));
		}

		super.writeJsonDate(ycsjService.datagrid(ycsj));
	}

	
	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}
	
	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	
	public void exportExcel() throws Exception {
		response.setContentType("application/msexcel;charset=GBK");
		SessionInfo s = (SessionInfo) request.getSession().getAttribute("sessionInfo");
		if (!StringUtils.isEmpty(ycsj.getDeptid())) {
			ycsj.setDeptIds(deptService.getChildIds(ycsj.getDeptid()));
		} else {
			ycsj.setDeptIds(deptService.getChildIds(s.getDeptid()));
		}
		List<Ycsj> list = ycsjService.excelData(ycsj);
		String title = "";
		if (!StringUtils.isEmpty(ycsj.getCbh())){
			title = userService.getNameByBh(ycsj.getCbh());
		}else{
			if (!StringUtils.isEmpty(ycsj.getDeptid())){
				title = deptService.getNameById(ycsj.getDeptid());
			}else{
				title = s.getDeptName();
			}
		}
		title += "异常营销数据表("+ycsj.getStartMonth()+"-"+ycsj.getEndMonth()+")";
		String[] hearders = new String[] { "部门", "编号","姓名", "月份", "账号", "业务种类",  "存期", "金额", "异常原因" };// 表头数组
		String[] fields = new String[] { "deptname", "cbh", "username", "csbyf", "czh",  "kindname", "ccq", "cye", "cyy" };// 对象属性数组
		int[] columnwidths = new int[] { 18, 10, 10, 12, 16, 12, 10, 16, 30 };
		TableData td = ExcelUtils.createTableData(list,ExcelUtils.createTableHeader(hearders), fields);
		JsGridReportBase report = new JsGridReportBase(request, response);
		report.exportToExcel(title, "admin", columnwidths, td);
	}
	


}
